
var baseUrl = getRoot() + "/websql";
var nowExecuteSQL = "";
var v_tableStructure = v_tableStructure;
var v_columns = [];
var v_primaryKeys = [];
var v_setColumns = [];
var orderColumns = [];
var whereConditionColumns = [];
var whereConditionSQL = "";
var option_btn_ids = ["add_btn", "delete_btn", "commit_btn"];
var binaryToHex = false;
var isBigTable = false;
var all_tableSize = 0;
/*if(v_tableSize){all_tableSize=v_tableSize["dataLength"]+v_tableSize["indexLength"];isBigTable=(all_tableSize>512)}*/
var instanceChangeCallBack = function() {};
var canEdit = function() {
    return v_primaryKeys.length != 0
};
var enableOptionButtons = function() {
    Ext.each(option_btn_ids,
    function(id) {
        var _option_button = Ext.getCmp(id);
        if (_option_button) {
            _option_button.enable()
        }
    })
};
var disableOptionButtons = function() {
    Ext.each(option_btn_ids,
    function(id) {
        var _option_button = Ext.getCmp(id);
        if (_option_button) {
            _option_button.disable()
        }
    })
};
var initStructure = function() {
    Ext.each(v_tableStructure,
    function(row) {
        v_columns.push(row["name"]);
        if (row["prikey"] == true) {
            v_primaryKeys.push(row["name"]);
            orderColumns.push({
                column: row["name"],
                order: "desc"
            });
        }
        whereConditionColumns.push({
            column: row["name"],
            computeMethod: "=",
            connectMethod: "and"
        })
    });
    if (!canEdit()) {
        disableOptionButtons();
        Ext.Msg.alert("提示", "该表没有主键，因此不能进行编辑操作！")
    }
};
var getCountSQL = function() {
    var sql;
    if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
        sql = "\n    select count(*) from " + nowDBName + "." + objectName + "";
    }else{
        sql = "\n    select count(*) from `" + nowDBName.replaceAll("`", "``") + "`.`" + objectName.replaceAll("`", "``") + "`";
    }
    if (whereConditionSQL) {
        sql += "\n    " + whereConditionSQL
    }
    return sql
};
var getNoLimitSQL = function() {
    var sql = "\n  select ";
    if (v_setColumns.length > 0) {
        Ext.each(v_setColumns,
        function(column) {
            if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
                sql += column + ","
            }else{
                sql += "`" + column.replaceAll("`", "``") + "`,"
            }
        });
        if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
            sql = sql.substring(0, sql.length - 1) + "\n    from "
        }else{
            sql = sql.substring(0, sql.length - 1) + "\n    from `"
        }
    } else {
        if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
            sql += "*\n  from "
        }else{
            sql += "*\n    from `"
        }
    }
    if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
        sql += currentDBName +"." + objectName +"";
    }else{
        sql += currentDBName.replaceAll("`", "``") + "`.`" + objectName.replaceAll("`", "``") + "`";
    }
    if (whereConditionSQL) {
        sql += "\n    " + whereConditionSQL
    }
    if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'mysql'){
        var orderSQL = getOrderSQL();
        if (orderSQL) {
            sql += "\n    " + orderSQL
        }
    }
    
    return sql
};
var initSQL = function() {
    var sql = getNoLimitSQL();
    var pageSize = Ext.getCmp("query_table_page_size");
    var pageNum = Ext.getCmp("query_table_page_num");
    pre_page_size = pageSize.getValue();
    pre_page_num = pageNum.getValue();
    var nowNum = (pre_page_num - 1) * pre_page_size;
    /*sql += "\n    limit " + nowNum + "," + pre_page_size;*/
    nowExecuteSQL = sql
};
Ext.onReady(function() {
    initStructure();
    var viewPort = Ext.create("Ext.Viewport", {
        layout: "border",
        items: [new Ext.Panel({
            region: "center",
            layout: "fit",
            items: [getTableResultPanel()]
        }), new Ext.TabPanel({
            activeTab: 0,
            headerStyle: "display:none",
            region: "east",
            width: 300,
            collapsible: true,
            split: true,
            title: "便捷操作区",
            tabPosition: "top",
            removePanelHeader: true,
            items: [{
                title: "返回列设置",
                layout: "fit",
                items: [returnColumnSetting()]
            },
            {
                title: "筛选条件",
                layout: "fit",
                items: [setWhereCondition()]
            },
            {
                title: "排序设置",
                layout: "fit",
                items: [setOrderByColumns()]
            }]
        })]
    });
    queryResult()
});
var showCancelWindow = function(sql, inputPanelKey) {
    var win_cancel = new Ext.Window({
        width: 500,
        height: 160,
        layout: "absolute",
        title: "请等待",
        plain: true,
        bodyStyle: "padding:5px;",
        buttonAlign: "center",
        html: '<span style="margin: 20 0 0 10;">正在执行SQL：</span><br/>  <span style="margin: 20 0 0 10;padding-top: 20px;"><pre>' + (sql || nowExecuteSQL) + "</pre></span>",
        modal: true,
        closable: false,
        buttons: [{
            text: "取消执行",
            handler: function() {
                win_cancel.hide();
                cancel(inputPanelKey)
            }
        }]
    });
    win_cancel.show();
    return win_cancel
};
function cancel(inputPanelKey) {
    ajax({
        url: baseUrl + "cancel.do",
        waitMsg: "请等待!",
        params: {
            event: "cancel",
            panelKey: (inputPanelKey || getPaneKey()),
            dbName: gridResult.currentDbName
        },
        failure: function(response) {},
        success: function(response) {
            var rs = Ext.decode(response.responseText)
        }
    })
}
function hasChangedResult() {
    if (canEdit()) {
        var deleteRecords = gridResult.getStore().getRemovedRecords();
        var editRecords = gridResult.getStore().getModifiedRecords();
        return (deleteRecords.length + editRecords.length) > 0
    }
    return false
}
var queryResult = function() {
    if (hasChangedResult()) {
        Ext.Msg.confirm("提示", "你的结果集有变更没有提交，如果继续执行新的SQL，没提交的变更将会丢失，是否继续?",
        function(v) {
            if (v == "yes") {
                queryResultDoingBefore()
            }
        })
    } else {
        queryResultDoingBefore()
    }
};
//打开表 start
var queryResultDoingBefore = function() {
    var pageSize = Ext.getCmp("query_table_page_size");
    var pageNum = Ext.getCmp("query_table_page_num");
    if (!pageSize.isValid()) {
        pageSize.setValue(pre_page_size)
    }
    if (!pageNum.isValid()) {
        pageNum.setValue(pre_page_num)
    }

    initSQL();
    queryResultDoing()
};
var queryResultDoing = function() {
    if (nowExecuteSQL) {
        var win_cancel = showCancelWindow();
        var url = baseUrl + "/executeSQL";
        var paramsValue;
        if(isLocal=='isLocal'){
            paramsValue={dataId: currentDataBaseId,baseType: currentBaseType,dataSourceId: currentDbSourceId,dataSource: cuurentdataSource,dbName: nowDBName,sql: nowExecuteSQL,pageNum: pre_page_num,pageSize: pre_page_size,isLocal:isLocal}
        }else{
            paramsValue={dataId: currentDataBaseId,baseType: currentBaseType,dataSourceId: currentDbSourceId,dataSource: cuurentdataSource,dbName: nowDBName,sql: nowExecuteSQL,pageNum: pre_page_num,pageSize: pre_page_size}
        }
        $.post(url,paramsValue,
        function(response){
            win_cancel.close();
            if (response.failure) {
                var errorMsg = response.root;
                Ext.Msg.show({
                    title: "执行出错",
                    msg: Ext.util.Format.htmlDecode(errorMsg),
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                })
            } else {
                showResult(response.root);
                /*if (rs.root.dataOverflow) {
                    Ext.MessageBox.alert("警告", "由于返回数据过大，剩余的数据未输出！")
                };*/
            }
        });
    }

};
function encodeValue(value, meta, record) {
    return '<pre style="padding: 0;margin:0;text-align:center;font-family:新宋体,Lucida Console,monospace">' + Ext.util.Format.htmlEncode(value) + "</pre>"
}
function isNull(value) {
    return (value == null)
}
var grid_fun = {
    findGridColumnNameByDBColumnName: function(dbColumnName) {
        var find = false,
        findColumnName = null;
        Ext.each(gridResult.metas,
        function(meta) {
            if (!find && meta["realName"] == dbColumnName) {
                findColumnName = meta["name"];
                find = true
            }
        });
        return findColumnName
    }
};
var row_fun = {
    checkPrimaryKeys: function(optionName) {
        if (v_primaryKeys.length > 0) {
            return true
        }
        Ext.Msg.alert("提示", "表没有主键，无法执行操作：【" + optionName + "】。");
        return false
    },
    getRowValue: function(key, row) {
        var realColumnName = grid_fun.findGridColumnNameByDBColumnName(key);
        var modified = row.modified;
        return modified[realColumnName] || row.get(realColumnName)
    },
    downloadBlob: function(colIndex) {
        var metaData = gridResult.metas[colIndex - 1];
        var metaDataColumnName = metaData["realName"];
        var selectedRows = gridResult.getSelectionModel().getSelection();
        if (selectedRows && selectedRows.length > 0 && row_fun.checkPrimaryKeys("下载BLOB")) {
            var v_primaryKeyValues = [],
            selectedRow = selectedRows[0];
            Ext.each(v_primaryKeys,
            function(key) {
                v_primaryKeyValues.push(row_fun.getRowValue(key, selectedRow))
            });
            Ext.getBody().mask("正在进行后台导出中，请等待....");
            ajax({
                url: "/data/queryTable/exportBlob.do",
                timeout: (15 * 60 * 1000),
                params: {
                    dbName: nowDBName,
                    tableName: objectName,
                    columnName: metaDataColumnName,
                    primaryKeys: v_primaryKeys,
                    primaryKeyValues: v_primaryKeyValues
                },
                success: function(resp) {
                    Ext.getBody().unmask();
                    var json = jsonDecode(resp.responseText);
                    if (json.success) {
                        downLoadFileByURL(json.root)
                    } else {
                        Ext.Msg.alert("提示", json.root)
                    }
                },
                failure: function() {
                    Ext.getBody().unmask()
                }
            })
        }
    },
    uploadBlob: function(colIndex) {
        var metaData = gridResult.metas[colIndex - 1];
        var metaDataColumnName = metaData["realName"];
        var selectedRows = gridResult.getSelectionModel().getSelection();
        if (selectedRows && selectedRows.length > 0 && row_fun.checkPrimaryKeys("上传BLOB")) {
            var items = [{
                xtype: "filefield",
                name: "file",
                fieldLabel: "附件",
                blankText: "附件不能为空",
                buttonText: "选择文件"
            },
            {
                name: "dbName",
                hidden: true,
                value: nowDBName
            },
            {
                name: "tableName",
                hidden: true,
                value: objectName
            },
            {
                name: "columnName",
                hidden: true,
                value: metaDataColumnName
            }];
            Ext.each(v_primaryKeys,
            function(key) {
                items.push({
                    name: "primaryKeys",
                    hidden: true,
                    value: key
                },
                {
                    name: "primaryKeyValues",
                    hidden: true,
                    value: row_fun.getRowValue(key, selectedRows[0])
                })
            });
            var form = new IDB.FormPanel({
                defaults: {
                    width: 540,
                    margin: "5 2 2 0",
                    columnWidth: 1
                },
                fileUpload: true,
                layout: "column",
                items: items
            });
            var win = new IDB.Window({
                width: 520,
                height: 115,
                title: "文件上传",
                items: [form],
                buttons: [{
                    text: "上传",
                    handler: function() {
                        if (form.isValid()) {
                            form.getForm().submit({
                                waitMsg: "正在导入数据，上传时间取决于文件大小及网速，请耐心等待....",
                                timeout: 10 * 60 * 1000,
                                url: "/data/queryTable/importBlob.do?token=" + getUserToken(),
                                success: function(fp, o) {
                                    var json = o.result;
                                    if (json.success) {
                                        queryResult();
                                        win.close()
                                    } else {
                                        if (json.root) {
                                            Ext.Msg.alert("服务器端错误", json.root)
                                        } else {
                                            if (json.indexOf("Request Entity Too Large") != -1) {
                                                Ext.Msg.alert("提示", "上传的数据太大，请检查你所上传的数据是否已经超过8M的限制。")
                                            }
                                        }
                                    }
                                },
                                failure: function(fp, o) {
                                    try {
                                        if (o.result.root) {
                                            Ext.Msg.alert("错误提示", o.result.root)
                                        } else {
                                            if (String(o.result).indexOf("Request Entity Too Large") != -1) {
                                                Ext.Msg.alert("提示", "上传的数据太大，请检查你所上传的数据是否已经超过8M的限制。")
                                            }
                                        }
                                    } catch(e) {
                                        Ext.Msg.alert("未知错误", jsonToString(o.result))
                                    }
                                }
                            })
                        }
                    }
                },
                {
                    text: "取消",
                    handler: function() {
                        win.close()
                    }
                }]
            })
        }
    }
};
function isNewRecord(record) {
    var newRecords = gridResult.store.getNewRecords();
    return (newRecords && newRecords.indexOf(record) != -1)
}
function rendererResultValue(value, meta, record, rowIndex, colIndex) {
    var columnName = "COLUMN_" + colIndex;
    var metaData = gridResult.metas[colIndex - 1];
    var metaDataType = metaData["dataType"];
    if (!isNull(value) && (record.raw.hasOwnProperty(columnName) || record.data[columnName] !== "")) {
        if (v_primaryKeys.length > 0 && metaDataType.indexOf("BLOB") != -1 && !isNewRecord(record)) {
            return value + "<br/><a href='javascript:row_fun.downloadBlob(" + colIndex + ")'>下载</a>" + " <a href='javascript:row_fun.uploadBlob(" + colIndex + ")'>上传</a>"
        }
        return encodeValue(value, meta, record)
    } else {
        if (v_primaryKeys.length > 0 && metaDataType.indexOf("BLOB") != -1 && !isNewRecord(record)) {
            return '<i style="color:#E5E5E5;">null</i>' + "<br/>" + " <a href='javascript:row_fun.uploadBlob(" + colIndex + ")'>上传</a>"
        }
        return '<i style="color:#E5E5E5;">null</i>'
    }
}
var showResult = function(result) {
    //console.log(result.metadata)
    var store = Ext.create("Ext.data.Store", {
        fields : result.metadata
    });
    var pageSize = Ext.getCmp("query_table_page_size").getValue();
    var pageNum = Ext.getCmp("query_table_page_num").getValue();
    var startIndex = (pageNum - 1) * pageSize;
    result.displayColumns[0].renderer = function(value, metadata, record,
            rowIndex) {
        return rowIndex + 1 + startIndex
    };
    gridResult.metas = result.metadata;
    for (var i = 1; i < result.displayColumns.length; i++) {
        result.displayColumns[i].renderer = rendererResultValue;
        result.displayColumns[i].sortable = false
    }
    gridResult.displayColumns = result.displayColumns;
    gridResult.data = result.data;
    //console.log(result)
    //var oldata=result.displayColumns;
    //var newdata={};
    /*for (var i=1,j=oldata.length; i<j;i++){
        newdata[oldata[i].dataIndex]=oldata[i].text
    }*/
    //console.log(newdata)
    //result.data.splice(0, 0, newdata)
    //console.log(result.data)
    //window.creatdata=result.data
    //result.displayColumns
    gridResult.reconfigure(store, result.displayColumns);
    store.loadRawData(result.data, true);
    var length = String(startIndex + 100).length;
    gridResult.getView().getHeaderCt(1).getHeaderAtIndex(0).setWidth(
            length * 10 + 8);
    moveRowResult("first");
    if (canEdit()) {
        enableOptionButtons();
        setColumnEditor()
    }
};
function moveRowResult(option) {
    var selectionModel = gridResult.getSelectionModel();
    if (option == "first") {
        selectionModel.select(0)
    } else {
        if (option == "last") {
            selectionModel.select(gridResult.store.getCount() - 1)
        } else {
            if (option == "previous") {
                selectionModel.select(gridResult.store.indexOf(selectionModel.lastSelected) - 1)
            } else {
                if (option == "next") {
                    selectionModel.select(gridResult.store.indexOf(selectionModel.lastSelected) + 1)
                }
            }
        }
    }
}
var pageSizeArray = [{
    data: 5
},
{
    data: 10
},
{
    data: 20
}];
var export_limit = false;
var isLimitExportUrl = false,
limitExportUrl = null;
var export_limit_rows = 0;
var export_limit_rows_name = "";
var isLimitAllExport = false;
if (window.parent) {
    if (window.parent.limitExport == "1" && window.parent.maxLimitRows) {
        export_limit = true;
        export_limit_rows = window.parent.maxLimitRows;
        export_limit_rows_name = window.parent.maxLimitRowsName
    }
    if (window.parent.limitAllExport == "1") {
        isLimitAllExport = true
    }
    if (window.parent.limitExportUrl) {
        isLimitExportUrl = true;
        limitExportUrl = window.parent.limitExportUrl;
        isLimitAllExport = true
    }
}
var gridResult;
var pre_page_num = 1,
pre_page_size = 10;




var getTableResultPanel = function() {
    Ext.define("ModelSqlResult", {
        extend: "Ext.data.Model",
        fields: []
    });
    var storeSqlResult = Ext.create("Ext.data.Store", {
        model: "ModelSqlResult",
        pageSize: 50,
        data: []
    });
    var pageSizeStore = new Ext.data.Store({
        fields: ["data"],
        data: pageSizeArray,
        proxy: {
            type: "memory"
        }
    });
    var cellEditing = Ext.create("Ext.grid.plugin.CellEditing", {
        clicksToEdit: 1
    });
    var export_menu = {
        text: "导出数据",
        handler: exportJob
    };
    if (isLimitExportUrl) {
        export_menu = {
            text: "导出数据",
            menu: [{
                text: "导出数据被屏蔽 点击查看详情",
                handler: function() {
                    window.parent.onbeforeunload = null;
                    window.parent.location = limitExportUrl
                }
            }]
        }
    } else {
        if (export_limit) {
            export_menu = {
                text: "导出数据",
                hidden: isLimitAllExport,
                menu: [{
                    text: "导出CSV（最多" + export_limit_rows_name + "）",
                    handler: function() {
                        exportNow("csv")
                    }
                },
                {
                    text: "导出Insert SQL（最多" + export_limit_rows_name + "）",
                    handler: function() {
                        exportNow("sql")
                    }
                }]
            }
        }
    }
    gridResult = Ext.create("Ext.grid.Panel", {
        store: storeSqlResult,
        region: "center",
        deferRowRender: false,
        sortableColumns: false,
        multiSelect: true,
        viewConfig: {
            stripeRows: true,
            enableTextSelection: true
        },
        columns: [Ext.create("Ext.grid.RowNumberer", {
            width: 30,
            renderer: function(value, metadata, record, rowIndex) {
                return rowIndex + 1
            }
        })],
        plugins: [cellEditing],
        listeners: {}
    });
    return new Ext.Panel({
        layout: "fit",
        items: [gridResult],
        bbar: [{
            text: "第一页",
            handler: function() {
                if (setFirstPage()) {
                    queryResult()
                }
            }
        },
        {
            text: "上一页",
            handler: function() {
                if (nextPage( - 1)) {
                    queryResult()
                }
            }
        },
        "当前页：", {
            xtype: "numberfield",
            allowDecimals: false,
            id: "query_table_page_num",
            width: 80,
            minValue: 1,
            value: pre_page_num,
            negativeText: "不允许输入负数",
            nanText: "无效数字字符"
        },
        {
            text: "GO",
            handler: function() {
                queryResult()
            }
        },
        {
            text: "下一页",
            handler: function() {
                if (nextPage(1)) {
                    queryResult()
                }
            }
        },
        "每页显示：", {
            xtype: "combo",
            id: "query_table_page_size",
            triggerAction: "all",
            displayField: "data",
            valueField: "data",
            queryMode: "local",
            forceSelection: false,
            editable: false,
            width: 80,
            typeAhead: true,
            value: pre_page_size,
            store: pageSizeStore,
            allowBlank: false,
            listeners: {
                select: function() {
                    queryResult()
                }
            }
        },
        "-", {
            text: "新增",
            iconCls: "add",
            disabled: true,
            id: "add_btn",
            handler: function() {
                cellEditing.completeEdit();
                addRecord()
            }
        },
        {
            text: "删除",
            iconCls: "delete",
            disabled: true,
            id: "delete_btn",
            handler: function() {
                cellEditing.completeEdit();
                deleteRecord()
            }
        },
        {
            text: "提交修改",
            iconCls: "accept",
            disabled: true,
            id: "commit_btn",
            handler: function() {
                cellEditing.completeEdit();
                showEditDataSql()
            }
        }],
        tbar: [{
            text: "刷新",
            iconCls: "fa fa-refresh",
            handler: queryResult
        },
        {
            text: "单行详情",
            handler: showRowContent
        },
        {
            text: "总行数",
            handler: showRowCount
        },
        {
            text: "模板SQL",
            menu: [{
                text: "Insert SQL",
                handler: showInsertSQLByRow
            },
            {
                text: "Update SQL",
                handler: showUpdateSQLByRow
            }]
        }
//        ,
//        {
//            text: "导出",
//            handler: exportData
//        }
//      {
//          xtype: "checkboxfield",
//          id: "binaryToHex",
//          boxLabel: "16进制显示Binary类型",
//          listeners: {
//              change: function(thiz, newValue, oldValue) {
//                  binaryToHex = newValue;
//                  queryResult()
//              }
//          }
//      }
        ]
    })
};
//var exportData=function(){
//    /** Execl导出方法 **/
//    new ExtGridToExcel("Excel标题",gridResult,12,'UTF-8')
//    
//    
//}
var showUpdateSQLByRow = function() {
    var selectionRow = gridResult.getSelectionModel().getSelection()[0];
    var sql = "update `" + objectName.replaceAll("`", "``") + "` set\n";
    var whereSQL = "where ";
    if (!selectionRow) {
        Ext.each(v_columns,
        function(v) {
            sql += "\t`" + v.replaceAll("`", "``") + "`=<" + v + ">,\n"
        });
        if (v_primaryKeys.length > 0) {
            Ext.each(v_primaryKeys,
            function(v) {
                whereSQL += "`" + v.replaceAll("`", "``") + "`=<" + v + "> and "
            });
            whereSQL = whereSQL.substring(0, whereSQL.length - 4)
        }
    } else {
        for (var i = 0; i < selectionRow.fields.items.length; i++) {
            var field = selectionRow.fields.items[i];
            if (field.name.substr(0, 7) == "COLUMN_") {
                var name = field.label;
                var data = selectionRow.data[field.name];
                var valueString = getFieldValueStr(data, field.dataType);
                sql += "\t`" + name.replaceAll("`", "``") + "`=" + valueString + ",\n";
                if (v_primaryKeys.length > 0 && v_primaryKeys.indexOf(name.toLowerCase()) != -1) {
                    whereSQL += "`" + name.replaceAll("`", "``") + "`=" + valueString + " and "
                }
            }
        }
        if (whereSQL.length > "where ".length) {
            whereSQL = whereSQL.substring(0, whereSQL.length - 4)
        }
    }
    sql = sql.substring(0, sql.length - 2) + "\n\t " + whereSQL;
    showTemplateSQL(sql, "Update SQL模板")
};
var showInsertSQLByRow = function() {
    var selectionRow = gridResult.getSelectionModel().getSelection()[0];
    var sql = "insert into `" + objectName.replaceAll("`", "``") + "` (";
    var values = "";
    if (!selectionRow) {
        Ext.each(v_columns,
        function(v) {
            sql += "`" + v.replaceAll("`", "``") + "`,";
            values += "<" + v + ">"
        });
        sql = sql.substring(0, sql.length - 1) + ")\nvalues(" + values + ")"
    } else {
        for (var i = 0; i < selectionRow.fields.items.length; i++) {
            var field = selectionRow.fields.items[i];
            if (field.name.substr(0, 7) == "COLUMN_") {
                var name = field.label;
                var data = selectionRow.data[field.name];
                sql += "`" + name.replaceAll("`", "``") + "`,";
                values += getFieldValueStr(data, field.dataType) + ","
            }
        }
        sql = sql.substring(0, sql.length - 1) + ")\nvalues(" + values.substring(0, values.length - 1) + ")"
    }
    showTemplateSQL(sql, "Insert SQL模板")
};
var showTemplateSQL = function(content, title) {
    var sqlArea = new Ext.ux.form.field.CodeMirror({
        value: content,
        autoScroll: true
    });
    var win = new IDB.Window({
        title: "表：[" + objectName + "]" + title,
        width: 600,
        height: 340,
        items: [sqlArea],
        buttons: [{
            text: "关闭",
            handler: function() {
                win.close()
            }
        }]
    })
};
var setFirstPage = function() {
    var pageNum = Ext.getCmp("query_table_page_num");
    if (pageNum.isValid() && pageNum.getValue() == 1) {
        Ext.Msg.alert("提示", "当前已经是第1页");
        return false
    }
    pageNum.setValue(1);
    return true
};
var nextPage = function(num) {
    var pageNum = Ext.getCmp("query_table_page_num");
    var nowNum = 0;
    if (!pageNum.isValid()) {
        nowNum = pre_page_num + num
    } else {
        nowNum = pageNum.getValue() + num
    }
    if (nowNum > 0) {
        pageNum.setValue(nowNum);
        return true
    } else {
        Ext.Msg.alert("提示", "当前已经是第1页");
        return false
    }
};
var showRowCount = function() {
    if (isBigTable) {
        Ext.Msg.confirm("提示", "表的容量较大,总容量为：" + all_tableSize + "MB，做count操作可能会占用大量的IO,导致数据库性能下降，是否继续？",
        function(v) {
            if (v == "yes") {
                executeCount()
            }
        })
    } else {
        executeCount()
    }
};
var temp_id = 1;
var executeCount = function() {
    var panelKey = getPaneKey() + (temp_id++);
    var sql = getCountSQL();
    var win_cancel = showCancelWindow(sql, panelKey);
    var url = baseUrl + "/executeSQL";
    var paramsValue;
    if(isLocal=='isLocal'){
        paramsValue={dataId: currentDataBaseId,dbName: nowDBName,baseType:currentBaseType,dataSourceId:currentDbSourceId,dataSource:cuurentdataSource,sql: sql,pageNum:pre_page_num,pageSize: pre_page_size,isLocal:isLocal}
    }else{
        paramsValue={dataId: currentDataBaseId,dbName: nowDBName,baseType:currentBaseType,dataSourceId:currentDbSourceId,dataSource:cuurentdataSource,sql: sql,pageNum:pre_page_num,pageSize: pre_page_size}
    }
    $.post(url,paramsValue,function(resp){
        win_cancel.close();
        if (resp.success) {
            Ext.Msg.alert("Count结果", "总行数为：" + resp.root.data[0].COLUMN_1 + "<br/>执行的SQL为：" + sql)
        }else{
            win_cancel.close()
        }
    });
};
var showRowContent = function() {
    var dataStore = new Ext.data.Store({
        fields: ["columnName", "columnValue", "dataType"]
    });
    var selectionRow = gridResult.getSelectionModel().getSelection()[0];
    if (!selectionRow) {
        return Ext.Msg.alert("提示", "没有选择任何数据!")
    }
    var setStoreData = function() {
        selectionRow = gridResult.getSelectionModel().getSelection()[0];
        dataStore.removeAll();
        for (var i = 0; i < selectionRow.fields.items.length; i++) {
            var field = selectionRow.fields.items[i];
            if (field.name.substr(0, 7) == "COLUMN_") {
                dataStore.add({
                    columnName: field.label,
                    columnValue: selectionRow.data[field.name],
                    dataType: getDataTypeStr(field.dataType, field.size)
                })
            }
        }
        updateRowResultStatus()
    };
    var grid = new Ext.grid.Panel({
        store: dataStore,
        region: "center",
        sortableColumns: false,
        deferRowRender: false,
        viewConfig: {
            stripeRows: true,
            enableTextSelection: true
        },
        columns: [Ext.create("Ext.grid.RowNumberer", {
            width: 30
        }), {
            text: "字段名",
            dataIndex: "columnName",
            width: 200
        },
        {
            text: "值",
            dataIndex: "columnValue",
            width: 500,
            renderer: rendererRowResultValue
        },
        {
            text: "类型",
            dataIndex: "dataType",
            width: 200
        }]
    });
    var win = new IDB.Window({
        title: "单行详情",
        items: [grid],
        width: 964,
        height: 420,
        maximizable: true,
        closable: true,
        buttons: [{
            text: "第一条",
            id: "bn_moveFirstRow",
            disabled: true,
            handler: function() {
                moveRowResult("first");
                setStoreData()
            }
        },
        {
            text: "上一条",
            id: "bn_movePreviousRow",
            handler: function() {
                moveRowResult("previous");
                setStoreData()
            }
        },
        {
            text: "下一条",
            id: "bn_moveNextRow",
            handler: function() {
                moveRowResult("next");
                setStoreData()
            }
        },
        {
            text: "最后一条",
            id: "bn_moveLastRow",
            handler: function() {
                moveRowResult("last");
                setStoreData()
            }
        }]
    });
    function updateRowResultStatus() {
        var selection = gridResult.getSelectionModel().getSelection()[0];
        if (selection) {
            var selectionId = gridResult.getStore().indexOf(selection);
            var isFirst = (selectionId <= 0);
            var isLast = (selectionId >= gridResult.store.getCount() - 1);
            if (isFirst) {
                Ext.getCmp("bn_moveFirstRow").disable();
                Ext.getCmp("bn_movePreviousRow").disable()
            } else {
                Ext.getCmp("bn_moveFirstRow").enable();
                Ext.getCmp("bn_movePreviousRow").enable()
            }
            if (isLast) {
                Ext.getCmp("bn_moveNextRow").disable();
                Ext.getCmp("bn_moveLastRow").disable()
            } else {
                Ext.getCmp("bn_moveNextRow").enable();
                Ext.getCmp("bn_moveLastRow").enable()
            }
        }
    }
    setStoreData()
};
function getDataTypeStr(dataType, size) {
    dataType = dataType + "(" + size + ")";
    return dataType.toLowerCase()
}
function rendererRowResultValue(value, meta, record, rowIndex, colIndex) {
    if (!isNull(value)) {
        return encodeValue(value, meta, record)
    } else {
        return '<i style="color:#E5E5E5;">null</i>'
    }
}
Array.prototype.indexOf = function(e) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == e) {
            return i
        }
    }
    return - 1
};
var createColumnStore = function() {
    var columnArray = [];
    Ext.each(v_columns,
    function(column) {
        columnArray.push({
            data: column
        })
    });
    return new Ext.data.Store({
        fields: ["data"],
        data: columnArray,
        proxy: {
            type: "memory"
        }
    })
};
var returnColumnSetting = function() {
    var grid = new Ext.grid.Panel({
        selType: "checkboxmodel",
        multiSelect: true,
        columns: [{
            text: "列名称",
            dataIndex: "data",
            sortable: false,
            flex: 1
        }],
        store: createColumnStore(),
        callBackQueryResult: function(selectedData) {
            v_setColumns = selectedData;
            queryResult()
        },
        buttonAlign: "center",
        buttons: [{
            text: "确定",
            handler: function() {
                var selectedData = grid.getSelectData();
                if (selectedData.length == 0) {
                    Ext.Msg.confirm("提示", "没有选择任何列，查询时将返回所有的列",
                    function(v) {
                        if (v == "yes") {
                            grid.callBackQueryResult(selectedData)
                        }
                    })
                } else {
                    if (v_primaryKeys.length == 0) {
                        grid.callBackQueryResult(selectedData)
                    } else {
                        var v_primary_copy = v_primaryKeys.slice(0);
                        Ext.each(selectedData,
                        function(row) {
                            var index = v_primary_copy.indexOf(row);
                            if (index != -1) {
                                v_primary_copy.splice(index, 1)
                            }
                        });
                        if (v_primary_copy.length > 0) {
                            var primaryKeyStr = "";
                            Ext.each(v_primary_copy,
                            function(row) {
                                primaryKeyStr += row + ","
                            });
                            Ext.Msg.alert("提示", "在选择的列中请包含主键列：[" + primaryKeyStr.substring(0, primaryKeyStr.length - 1) + "]")
                        } else {
                            grid.callBackQueryResult(selectedData)
                        }
                    }
                }
            }
        }],
        getSelectData: function() {
            var selectedRows = grid.getSelectionModel().getSelection();
            var selectedArray = [];
            Ext.each(selectedRows,
            function(record) {
                selectedArray.push(record.get("data"))
            });
            return selectedArray
        },
        listeners: {
            afterrender: function() {
                var preSelectRecords = [];
                if (v_setColumns.length > 0) {
                    grid.store.each(function(record) {
                        if (v_setColumns.indexOf(record.get("data")) != -1) {
                            preSelectRecords.push(record)
                        }
                    })
                } else {
                    grid.store.each(function(record) {
                        preSelectRecords.push(record)
                    })
                }
                if (preSelectRecords.length > 0) {
                    grid.getSelectionModel().select(preSelectRecords)
                }
            }
        }
    });
    return grid
};
var setOrderByColumns = function() {
    var columnStore = createColumnStore();
    var orderStore = new Ext.data.Store({
        fields: ["data"],
        data: [{
            data: "asc"
        },
        {
            data: "desc"
        }],
        proxy: {
            type: "memory"
        }
    });
    var cellEditing = new Ext.grid.plugin.CellEditing({
        clicksToEdit: 1
    });
    var grid = new Ext.grid.Panel({
        selType: "rowmodel",
        plugins: [cellEditing],
        columns: [{
            xtype: "rownumberer"
        },
        {
            text: "排序列",
            dataIndex: "column",
            sortable: false,
            flex: 1,
            editor: {
                xtype: "combo",
                triggerAction: "all",
                displayField: "data",
                valueField: "data",
                queryMode: "local",
                forceSelection: true,
                typeAhead: true,
                store: columnStore,
                allowBlank: false
            }
        },
        {
            text: "排序方式",
            dataIndex: "order",
            sortable: false,
            width: 120,
            editor: {
                xtype: "combo",
                triggerAction: "all",
                displayField: "data",
                valueField: "data",
                queryMode: "local",
                forceSelection: true,
                typeAhead: true,
                store: orderStore,
                allowBlank: false
            }
        }],
        buttonAlign: "center",
        buttons: [{
            text: "确定",
            handler: function() {
                var columns = getOrderColumns();
                if (columns.length == 0) {
                    return Ext.Msg.confirm("提示", "没有排序条件，在SQL查询时将不会有ORDER操作，是否继续？",
                    function(v) {
                        if (v == "yes") {
                            Ext.getCmp("order_sql").setValue("");
                            callBackQueryResult([])
                        }
                    })
                } else {
                    var sql = getOrderSQL(columns);
                    if (sql && sql.length > 0) {
                        Ext.getCmp("order_sql").setValue(sql)
                    } else {
                        Ext.getCmp("order_sql").setValue("")
                    }
                    callBackQueryResult(columns)
                }
            }
        },
        {
            text: '<img src="../img/iconfont-iconjia.png" height="15">',
            width: 40,
            handler: function() {
                grid.store.add({
                    order: "asc"
                });
                selectLast()
            }
        },
        {
            text: '<img src="../img/iconfont-jian.png" height="15">',
            width: 40,
            handler: function() {
                var selectedRows = grid.getSelectionModel().getSelection();
                if (!selectedRows || selectedRows.length == 0) {
                    Ext.Msg.alert("提示", "请先选择要删除的行。");
                    return
                }
                Ext.each(selectedRows,
                function(row) {
                    grid.store.remove(row)
                });
                grid.getView().refresh();
                selectLast()
            }
        }],
        store: new Ext.data.Store({
            fields: ["column", "order"],
            data: orderColumns[0] ? orderColumns[0] : [],
            proxy: {
                type: "memory"
            }
        }),
        listeners: {
            afterrender: function() {
                grid.store.add({
                    order: "asc"
                })
            }
        }
    });
    var selectLast = function() {
        var selectedRows = grid.getSelectionModel().getSelection();
        if (grid.store.getCount() > 0) {
            grid.getSelectionModel().select(grid.store.last())
        }
    };
    var getOrderColumns = function() {
        var columns = [];
        grid.store.each(function(record) {
            if (Ext.String.trim(record.get("column"))) {
                columns.push(record.data)
            }
        });
        return columns
    };
    var callBackQueryResult = function(columns) {
        orderColumns = columns;
        queryResult()
    };
    var board = new Ext.Panel({
        height: 100,
        layout: "border",
        defaults: {
            collapsible: false,
            split: true,
            bodyStyle: "padding:0px"
        },
        items: [{
            xtype: "codemirror",
            id: "order_sql",
            name: "order",
            border: 0,
            readOnly: true,
            region: "center",
            value: ""
        }]
    });
    var res = new Ext.Panel({
        layout: "border",
        defaults: {
            collapsible: false,
            split: true,
            bodyStyle: "padding:0px"
        },
        items: [{
            region: "center",
            layout: "fit",
            items: [grid]
        },
        {
            region: "south",
            layout: "fit",
            items: [board]
        }]
    });
    return res
};
var getOrderSQL = function(v_orderColumns) {
    var realOrderColumns = v_orderColumns || orderColumns;
    if (realOrderColumns.length == 0) {
        return ""
    }
    var orderSQL = "order by ";
    Ext.each(realOrderColumns,
    function(row) {
        if(currentBaseType.toLowerCase() == 'oracle' || currentBaseType.toLowerCase() == 'drds'){
            orderSQL += "" + row["column"] + " " + row["order"] + ","
        }else{
            orderSQL += "`" + row["column"].replaceAll("`", "``") + "` " + row["order"] + ","
        }
    });
    return orderSQL.substring(0, orderSQL.length - 1)
};
var compareMethods = [{
    data: "=",
    label: "等于"
},
{
    data: "in",
    label: "in"
},
{
    data: "!=",
    label: "不等于"
},
{
    data: "<",
    label: "小于"
},
{
    data: "<=",
    label: "小于或等于"
},
{
    data: ">",
    label: "大于"
},
{
    data: ">=",
    label: "大于或等于"
},
{
    data: "like",
    label: "like"
},
{
    data: "like begin",
    label: "like前缀",
    realData: "like"
},
{
    data: "not like",
    label: "not like"
},
{
    data: "not like begin",
    label: "not like前缀",
    realData: "not like"
},
{
    data: "is null",
    label: "是空值"
},
{
    data: "is not null",
    label: "不是空值"
}];
var compareMethodMap = {};
for (var i = 0; i < compareMethods.length; i++) {
    var row = compareMethods[i];
    compareMethodMap[row["data"]] = row["realData"] || row["data"]
}
var createCompareMethodStore = function() {
    return new Ext.data.Store({
        fields: ["data", "label", "realData"],
        data: compareMethods,
        proxy: {
            type: "memory"
        }
    })
};
var createConnectMethodStore = function() {
    return new Ext.data.Store({
        fields: ["data", "label"],
        data: [{
            data: "and"
        },
        {
            data: "or"
        }],
        proxy: {
            type: "memory"
        }
    })
};
var getCopyWhereConditionColumn = function() {
    var copyWhereConditionColumns = [];
    Ext.each(whereConditionColumns,
    function(row) {
        var newRow = {};
        Ext.apply(newRow, row);
        copyWhereConditionColumns.push(newRow)
    });
    return copyWhereConditionColumns
};
var _DB_TYPE=dbType;
var whereConditionPanel = null;
var setWhereCondition = function() {
    var columnStore = createColumnStore();
    var compareMethodStore = createCompareMethodStore();
    var connectMethodStore = createConnectMethodStore();
    var cellEditing = new Ext.grid.plugin.CellEditing({
        clicksToEdit: 1
    });
    var selectLast = function() {
        var selectedRows = grid.getSelectionModel().getSelection();
        if (grid.store.getCount() > 0) {
            grid.getSelectionModel().select(grid.store.last())
        }
    };
    var grid = whereConditionPanel = new Ext.grid.Panel({
        selType: "rowmodel",
        plugins: [cellEditing],
        columns: [{
            text: "条件列",
            dataIndex: "column",
            sortable: false,
            width: 110,
            editor: {
                xtype: "combo",
                triggerAction: "all",
                displayField: "data",
                valueField: "data",
                queryMode: "local",
                forceSelection: true,
                typeAhead: true,
                store: columnStore,
                allowBlank: false
            }
        },
        {
            text: "关系",
            dataIndex: "computeMethod",
            sortable: false,
            width: 60,
            editor: {
                xtype: "combo",
                triggerAction: "all",
                displayField: "label",
                valueField: "data",
                queryMode: "local",
                forceSelection: true,
                editable: false,
                typeAhead: true,
                store: compareMethodStore,
                allowBlank: false,
                listeners: {
                    select: function() {}
                }
            },
            renderer: function(v) {
                if (v) {
                    var record = grid.compareMethodStore.findRecord("data", v);
                    return record.get("label")
                }
                return v
            }
        },
        {
            text: "值",
            dataIndex: "values",
            sortable: false,
            width: 130,
            editor: {
                xtype: "textfield"
            },
            renderer: function(v, css, row) {
                var data = row.data;
                if (data["computeMethod"] == "in") {
                    return v + '<a href="javascript:whereConditionPanel.editColumnValue()">值列表</a>'
                }
                return v
            }
        },
        {
            text: "连接符",
            dataIndex: "connectMethod",
            sortable: false,
            width: 70,
            editor: {
                xtype: "combo",
                triggerAction: "all",
                displayField: "data",
                valueField: "data",
                queryMode: "local",
                forceSelection: true,
                editable: false,
                typeAhead: true,
                store: connectMethodStore,
                allowBlank: false
            }
        }],
        buttonAlign: "center",
        buttons: [{
            text: "确定",
            width: 40,
            handler: function() {
                var filterColumns = grid.getFilterColumns();
                if (filterColumns.length == 0) {
                    whereConditionSQL = "";
                    Ext.getCmp("where_sql").setValue("");
                    whereConditionColumns = [];
                    queryResult()
                } else {
                    var conditionSQLObject = getWhereConditionSQL(filterColumns, compareMethodStore);
                    if (conditionSQLObject.status) {
                        whereConditionSQL = conditionSQLObject.whereCondition;
                        if (whereConditionSQL && whereConditionSQL.length > 0) {
                            Ext.getCmp("where_sql").setValue(whereConditionSQL)
                        } else {
                            Ext.getCmp("where_sql").setValue("")
                        }
                        whereConditionColumns = filterColumns;
                        queryResult()
                    }
                }
            }
        },
        {
            text: '<img src="../img/iconfont-iconjia.png" height="15">',
            width: 10,
            handler: function() {
                grid.store.add({
                    computeMethod: "=",
                    connectMethod: "and"
                });
                selectLast()
            }
        },
        {
            text: '<img src="../img/iconfont-jian.png" height="15">',
            width: 10,
            handler: function() {
                var selectedRows = grid.getSelectionModel().getSelection();
                if (!selectedRows || selectedRows.length == 0) {
                    Ext.Msg.alert("提示", "请先选择要删除的行。");
                    return
                }
                Ext.each(selectedRows,
                function(row) {
                    grid.store.remove(row)
                });
                grid.getView().refresh();
                selectLast()
            }
        }],
        store: new Ext.data.Store({
            fields: ["column", "computeMethod", "values", "connectMethod"],
            data: getCopyWhereConditionColumn(),
            proxy: {
                type: "memory"
            }
        }),
        listeners: {
            afterrender: function() {
                grid.store.add({
                    computeMethod: "=",
                    connectMethod: "and"
                })
            },
            beforeEdit: function(grid, e) {
                var computeMethod = e.record.get("computeMethod");
                if ((computeMethod == "in" || computeMethod == "is null" || computeMethod == "is not null") && e.column.dataIndex == "values") {
                    return false
                }
                return true
            }
        },
        editColumnValue: function() {
            var row = grid.getSelectionModel().getSelection()[0];
            editColumnValuePanel(row)
        },
        getFilterColumns: function() {
            var columnArray = [];
            grid.store.each(function(record) {
                if (record.get("column")) {
                    columnArray.push(record.data)
                }
            });
            return columnArray
        }
    });
    grid.compareMethodStore = compareMethodStore;
    var editColumnValuePanel = function(row) {
        var values = row.get("values");
        var valueArray = splitValues(values);
        var valueRowsData = [];
        Ext.each(valueArray,
        function(row) {
            valueRowsData.push({
                value: row
            })
        });
        var columnGrid = new Ext.grid.Panel({
            selType: "rowmodel",
            plugins: [new Ext.grid.plugin.CellEditing({
                clicksToEdit: 1
            })],
            columns: [{
                xtype: "rownumberer"
            },
            {
                text: "值",
                dataIndex: "value",
                sortable: false,
                flex: 1,
                editor: {
                    xtype: "textfield"
                }
            }],
            store: new Ext.data.Store({
                fields: ["value"],
                data: valueRowsData,
                proxy: {
                    type: "memory"
                }
            }),
            listeners: {
                afterrender: function() {
                    columnGrid.store.add({})
                }
            }
        });
        var selectLast2 = function() {
            var selectedRows = columnGrid.getSelectionModel().getSelection();
            if (columnGrid.store.getCount() > 0) {
                columnGrid.getSelectionModel().select(columnGrid.store.last())
            }
        };
        var windowInner = new IDB.Window({
            title: "请输入包含的值列表",
            height: 450,
            width: 450,
            closable: true,
            items: [columnGrid],
            buttons: [{
                text: '<img src="../img/iconfont-iconjia.png" height="15">',
                width: 40,
                handler: function() {
                    columnGrid.store.add({});
                    selectLast2()
                }
            },
            {
                text: '<img src="../img/iconfont-jian.png" height="15">',
                width: 40,
                handler: function() {
                    var selectedRows = columnGrid.getSelectionModel().getSelection();
                    if (!selectedRows || selectedRows.length == 0) {
                        Ext.Msg.alert("提示", "请先选择要删除的行。");
                        return
                    }
                    Ext.each(selectedRows,
                    function(row) {
                        columnGrid.store.remove(row)
                    });
                    columnGrid.getView().refresh();
                    selectLast2()
                }
            },
            {
                text: "确定",
                handler: function() {
                    var values = "";
                    columnGrid.store.each(function(record) {
                        if (record.get("value")) {
                            values += "'" + record.get("value").replaceAll("'", "''") + "',"
                        }
                    });
                    if (values.length > 0) {
                        values = values.substring(0, values.length - 1)
                    }
                    row.set("values", values);
                    windowInner.close()
                }
            },
            {
                text: "取消",
                handler: function() {
                    windowInner.close()
                }
            }]
        })
    };
    var board = new Ext.Panel({
        height: 100,
        layout: "border",
        defaults: {
            collapsible: false,
            split: true,
            bodyStyle: "padding:0px"
        },
        items: [{
            xtype: "codemirror",
            id: "where_sql",
            name: "where",
            border: 0,
            readOnly: true,
            region: "center",
            value: ""
        }]
    });
    var res = new Ext.Panel({
        layout: "border",
        defaults: {
            collapsible: false,
            split: true,
            bodyStyle: "padding:0px"
        },
        items: [{
            region: "center",
            layout: "fit",
            items: [grid]
        },
        {
            region: "south",
            layout: "fit",
            items: [board]
        }]
    });
    return res
};
var getWhereConditionSQL = function(columns, compareMethodStore) {
    var initWhereCondition = "where ";
    var whereCondition = initWhereCondition,
    preConnect = "";
    var status = true;
    Ext.each(columns,
    function(row) {
        if (!status) {
            return
        }
        var values = row["values"],
        computeMethod = row["computeMethod"];
        if (!values && computeMethod != "is null" && computeMethod != "is not null") {
            return
        }
        whereCondition += preConnect + " `" + row["column"].replaceAll("`", "``") + "` " + compareMethodMap[computeMethod];
        if (computeMethod == "=" || computeMethod == "!=") {
            if (values.charAt(0) != "'" && values.charAt(0) != '"' && values.substring(0, 2) != "0x") {
                values = "'" + values.replaceAll("'", "''") + "'"
            }
            whereCondition += values
        } else {
            if (computeMethod == ">" || computeMethod == "<" || computeMethod == ">=" || computeMethod == "<=") {
                whereCondition += values
            } else {
                if (computeMethod == "like" || computeMethod == "not like") {
                    if (values.charAt(0) == "'" || values.charAt(0) == '"') {
                        values = values.substring(1, values.length - 1)
                    }
                    whereCondition += "'%" + values + "%'"
                } else {
                    if (computeMethod == "like begin" || computeMethod == "not like begin") {
                        if (values.charAt(0) == "'" || values.charAt(0) == '"') {
                            values = values.substring(1, values.length - 1)
                        }
                        whereCondition += "'" + values + "%'"
                    } else {
                        if (computeMethod == "in") {
                            whereCondition += "(" + values + ")"
                        }
                    }
                }
            }
        }
        whereCondition += "\n      ";
        preConnect = row["connectMethod"]
    });
    if (whereCondition == initWhereCondition) {
        whereCondition = ""
    }
    return {
        whereCondition: whereCondition,
        status: status
    }
};
var splitValues = function(values) {
    var valueArray = [];
    if (values) {
        var beginIndex = 0;
        var comm1 = false,
        comm2 = false;
        var valueLength = values.length;
        for (var i = 0; i < valueLength; i++) {
            var c = values.charAt(i);
            if (comm1 && c == "'") {
                if ((i + 1) < valueLength && values.charAt(i + 1) != "'") {
                    comm1 = false
                } else {
                    i++
                }
            } else {
                if (comm2 && c == '"') {
                    if ((i + 1) < valueLength && values.charAt(i + 1) != '"') {
                        comm2 = false
                    } else {
                        i++
                    }
                } else {
                    if (c == "\\" && (comm1 || comm2)) {
                        i++
                    } else {
                        if (!comm1 && !comm2) {
                            if (c == "'") {
                                comm1 = true
                            } else {
                                if (c == '"') {
                                    comm2 = true
                                } else {
                                    if (c == ",") {
                                        valueArray.push(clearData(values.substring(beginIndex, i)));
                                        beginIndex = i + 1
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (beginIndex != valueLength) {
            valueArray.push(clearData(values.substring(beginIndex, valueLength)))
        }
    }
    return valueArray
};
var clearData = function(data) {
    if (data) {
        if (data.charAt(0) == "'") {
            data = data.substring(1, data.length - 1);
            data = data.replaceAll("''", "'");
            data = data.replaceAll("\\'", "'")
        } else {
            if (data.charAt(0) == '"') {
                data = data.substring(1, data.length - 1);
                data = data.replaceAll('""', '"');
                data = data.replaceAll('\\"', '"')
            }
        }
    }
    return data
};
var fileTypeArray2 = [{
    data: "CSV",
    label: "CSV"
},
{
    data: "SQL_Insert",
    label: "SQL_Insert"
}];
var fileCharsetTypeArray = [{
    data: "utf8"
},
{
    data: "gbk"
}];
var rowsLimitArray = [{
    data: "10000",
    label: "1万"
},
{
    data: "50000",
    label: "5万"
},
{
    data: "100000",
    label: "10万"
},
{
    data: "200000",
    label: "20万"
},
{
    data: "500000",
    label: "50万"
},
{
    data: "1000000",
    label: "100万"
},
{
    data: "2000000",
    label: "200万"
},
{
    data: "5000000",
    label: "500万"
}];
var exportNow = function(format) {
    var sql = getNoLimitSQL();
    openPostWindow(baseUrl + "export.do?format=" + format + "&panelKey=export&tableName=" + objectName + "&dbName=" + nowDBName + "&rows=" + export_limit_rows + "&_nocache=" + Math.random() + "&token=" + getUserToken(), sql, "_blank")
};
function openPostWindow(url, sql, name) {
    var tempForm = document.createElement("form");
    tempForm.id = "tempForm1";
    tempForm.method = "post";
    tempForm.action = "/mysql" + url;
    tempForm.target = name;
    var hideInput = document.createElement("input");
    hideInput.type = "hidden";
    hideInput.name = "sql";
    hideInput.value = sql;
    tempForm.appendChild(hideInput);
    document.body.appendChild(tempForm);
    tempForm.submit();
    document.body.removeChild(tempForm)
}
var exportJob = function() {
    var sql = getNoLimitSQL();
    var form = new IDB.FormPanel({
        region: "center",
        layout: "column",
        defaults: {
            margin: "2 2 2 0",
            width: 500,
            columnWidth: 1,
            labelWidth: 100
        },
        items: [{
            columnWidth: 0.5,
            width: 230,
            fieldLabel: "文件类型",
            editable: false,
            blankText: "文件类型不能为空",
            xtype: "combo",
            name: "fileType",
            triggerAction: "all",
            displayField: "label",
            valueField: "data",
            queryMode: "local",
            forceSelection: true,
            typeAhead: true,
            value: "CSV",
            store: new Ext.data.Store({
                fields: ["data", "label"],
                data: fileTypeArray2
            })
        },
        {
            columnWidth: 0.5,
            fieldLabel: "文件字符集",
            editable: false,
            blankText: "字符集选项不能为空",
            xtype: "combo",
            name: "fileCharset",
            triggerAction: "all",
            displayField: "data",
            valueField: "data",
            queryMode: "local",
            value: "gbk",
            forceSelection: true,
            typeAhead: true,
            store: new Ext.data.Store({
                fields: ["data"],
                data: fileCharsetTypeArray
            })
        },
        {
            xtype: "checkboxgroup",
            fieldLabel: "选项",
            id: "check_option",
            columns: 1,
            items: [{
                name: "compressInsert",
                inputValue: "Y",
                hidden: true,
                checked: true,
                boxLabel: "insert语句合并（以5M为单位将多个insert语句进行合并）"
            },
            {
                name: "enableCsvTitleRow",
                inputValue: "Y",
                checked: true,
                boxLabel: "包含属性行（选中此项CSV第1行为属性名称，否则第1行为数据）"
            }]
        },
        {
            fieldLabel: "结果集行数限制",
            editable: false,
            xtype: "combo",
            name: "tableRowsLimit",
            triggerAction: "all",
            displayField: "label",
            valueField: "data",
            queryMode: "local",
            value: "200000",
            forceSelection: true,
            typeAhead: true,
            store: new Ext.data.Store({
                fields: ["data", "label"],
                data: rowsLimitArray
            })
        },
        {
            fieldLabel: "数据库",
            name: "dbName",
            blankText: "数据库选项不能为空",
            readOnly: true,
            value: nowDBName
        },
        {
            fieldLabel: "执行SQL",
            xtype: "textarea",
            height: 90,
            maxLength: 20000,
            emptyText: "请在这里输入1条Select语句....",
            maxLengthText: "SQL请不要超过20000个字符",
            minLength: 8,
            minLengthText: "SQL语句太短",
            name: "sql",
            value: Ext.String.trim(sql),
            allowBlank: false,
            blankText: "SQL语句不能为空"
        },
        {
            fieldLabel: "描述",
            xtype: "textarea",
            height: 60,
            maxLength: 1024,
            maxLengthText: "请不要超过1024个字符",
            name: "createDesc",
            allowBlank: true
        }]
    });
    var fileTypeField = form.getForm().findField("fileType");
    var setExportCreateScript = function() {
        if (fileTypeField.getValue() == "SQL_Insert") {
            form.getForm().findField("compressInsert").show();
            form.getForm().findField("enableCsvTitleRow").hide()
        } else {
            form.getForm().findField("compressInsert").hide();
            form.getForm().findField("enableCsvTitleRow").show()
        }
    };
    var submitTO = function() {
        if (form.isValid()) {
            var params = form.getValues();
            params["exportMode"] = "sql";
            params["exportMethod"] = "data";
            Ext.apply(params, advanceWindow.getAdvanceValues());
            win.getEl().mask("正在提交任务，请等待....");
            ajax({
                url: "/data/export/saveExportJobByQueryTable.do",
                params: params,
                success: function(resp) {
                    win.getEl().unmask();
                    var json = jsonDecode(resp.responseText);
                    if (json.success) {
                        showMonitor(json.root);
                        win.close()
                    } else {
                        Ext.Msg.alert("错误提示", json.root)
                    }
                },
                failure: function() {
                    win.getEl().unmask()
                }
            })
        }
    };
    fileTypeField.on("select", setExportCreateScript);
    var win = new IDB.Window({
        title: "新增SQL导出任务",
        width: 850,
        height: 420,
        closable: true,
        layout: "fit",
        items: [form],
        buttons: [{
            text: "高级选项",
            handler: function() {
                advanceWindow.show()
            }
        },
        {
            text: "确定",
            handler: submitTO
        },
        {
            text: "关闭",
            handler: function() {
                win.close()
            }
        }]
    });
    var advanceWindow = createAdvanceWindow();
    win.on("beforedestroy",
    function() {
        advanceWindow.close()
    })
};
var createAdvanceWindow = function() {
    var form = new IDB.FormPanel({
        defaults: {
            bodyPadding: 5,
            padding: "0 5 0 5",
            xtype: "fieldset",
            width: 520,
            defaults: {
                autoFitErrors: false,
                labelSeparator: " : ",
                labelWidth: 80,
                width: 500,
                labelAlign: "right",
                xtype: "textfield"
            }
        },
        items: [{
            title: "数据选项",
            items: [{
                xtype: "checkboxfield",
                name: "exportBlob",
                inputValue: "Y",
                hiddenLabel: true,
                checked: false,
                boxLabel: "导出TINYBLOB、BLOB、MEDIUMBLOB、LONGBLOB类型（输出格式为16进制）"
            },
            {
                xtype: "checkboxfield",
                name: "exportBinary",
                inputValue: "Y",
                hiddenLabel: true,
                checked: true,
                boxLabel: "导出BINARY、VARBINARY类型（输出格式为16进制）"
            },
            {
                xtype: "checkboxfield",
                name: "exportClob",
                inputValue: "Y",
                hiddenLabel: true,
                checked: true,
                boxLabel: "导出TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT类型"
            }]
        }]
    });
    var win = new Ext.window.Window({
        height: 220,
        width: 600,
        title: "高级选项",
        modal: true,
        layout: "fit",
        closable: false,
        buttonAlign: "center",
        items: [form],
        buttons: [{
            text: "确定",
            handler: function() {
                win.hide()
            }
        }],
        getAdvanceValues: function() {
            return form.getValues()
        }
    });
    return win
};
var showMonitor = function(id) {
    ajax({
        url: "/data/export/monitorExportJob.do?id=" + id + "&firstTime=Y",
        success: function(resp) {
            var json = jsonDecode(resp.responseText);
            if (json.success) {
                showMonitorDoing(json.root, id)
            } else {
                Ext.Msg.alert("提示", json.root)
            }
        }
    })
};
var showMonitorDoing = function(json, id) {
    if (json["exportStatus"] == "END" && json["exportMode"] != "sql") {
        return showDetail(id)
    }
    var use = getProcess(json);
    var sql = json["sql"];
    var processBar = new Ext.ProgressBar({
        width: 500,
        height: 45,
        text: "当前进度：" + use + "%",
        paddingTop: 5,
        region: "north"
    });
    processBar.updateProgress(parseFloat(use) / 100, "当前进度：" + use + "%<br/>已导出总行数：" + json["nowRows"], true);
    var win = new IDB.Window({
        title: "任务进度跟踪(每2s刷新一次进度)",
        layout: "border",
        width: 680,
        height: 450,
        items: [{
            region: "north",
            height: 50,
            border: false,
            layout: "fit",
            items: [processBar],
            id: "processBarArea"
        },
        {
            html: "详情",
            region: "center",
            layout: "fit",
            border: false,
            baseCls: "log_content",
            id: "oneTableLog",
            xtype: "container"
        }],
        buttons: [{
            text: "关闭",
            handler: function() {
                win.close()
            }
        },
        {
            hidden: true,
            id: "monitor_download_file",
            text: "下载文件",
            handler: function() {
                downloadAllFile(id)
            }
        }]
    });
    var nowLogContentDom = Ext.getCmp("oneTableLog").el.dom;
    nowLogContentDom.innerHTML = "<pre>" + getMonitorBaseInfoByRow(json, new Date()) + (json["log"] || "") + "</pre>";
    var task = {
        interval: 2000,
        run: function() {
            var date = new Date();
            ajax({
                url: "/data/export/monitorExportJob.do?id=" + id,
                success: function(resp) {
                    var json = jsonDecode(resp.responseText);
                    if (json.success) {
                        json = json.root;
                        json["sql"] = sql;
                        var use = getProcess(json);
                        processBar.updateProgress(parseFloat(use) / 100, "当前进度：" + use + "%<br/>已导出总行数：" + json["nowRows"], true);
                        nowLogContentDom.innerHTML = "<pre>" + getMonitorBaseInfoByRow(json, new Date()) + (json["log"] || "") + "</pre>";
                        nowLogContentDom.scrollTop = nowLogContentDom.scrollHeight;
                        if (json["exportStatus"] == "END") {
                            Ext.TaskManager.stop(task);
                            win.setTitle("详情");
                            Ext.getCmp("monitor_download_file").show();
                            downloadAllFile(id)
                        } else {
                            if (json["exportStatus"] == "ERROR") {
                                Ext.TaskManager.stop(task);
                                win.setTitle("查看错误信息");
                                Ext.getCmp("processBarArea").hide()
                            }
                        }
                    } else {
                        Ext.Msg.alert("提示", json.root)
                    }
                }
            })
        }
    };
    if (json["exportStatus"] != "ERROR") {
        Ext.TaskManager.start(task);
        win.on("beforedestroy",
        function() {
            Ext.TaskManager.stop(task)
        })
    } else {
        win.setTitle("查看错误信息");
        Ext.getCmp("processBarArea").hide()
    }
};
var getMonitorBaseInfoByRow = function(row, date) {
    if (row["exportMode"] == "sql") {
        return "======================================================================\n" + "     当前状态：" + Ext.util.Format.htmlDecode(row["statusName"]) + "\n" + "     刷新时间：" + Ext.util.Format.date(date, "Y-m-d H:i:s") + "\n" + "     " + Ext.util.Format.htmlDecode(row["nowLog"] || "等待执行....") + "\n" + "     SQL语句：\n" + "     " + String(row["sql"]).replaceAll("\n", "\n     ") + "\n" + "======================================================================\n"
    }
    return "======================================================================\n" + "     当前状态：" + Ext.util.Format.htmlDecode(row["statusName"]) + "\n" + "     已导出表：" + Ext.util.Format.htmlDecode(row["exportedTabs"]) + " / " + Ext.util.Format.htmlDecode(row["exportTabNum"]) + "\n" + "     刷新时间：" + Ext.util.Format.date(date, "Y-m-d H:i:s") + "\n" + "     " + Ext.util.Format.htmlDecode(row["nowLog"] || "等待执行....") + "\n" + "======================================================================\n"
};
var getProcess = function(data) {
    var use = 0;
    if (data["exportStatus"] == "END") {
        return 100
    }
    if (data["preRows"] && data["exportStatus"] != "END") {
        use = ((parseFloat(data["nowRows"]) / parseInt(data["preRows"])) * 100).toFixed(0);
        if (use >= 100) {
            use = 99
        }
    } else {
        use = ((parseFloat(data["completeSize"]) / parseInt(data["exportTabSize"])) * 100).toFixed(0)
    }
    return use
};
var downloadAllFile = function(id) {
    downLoadFileByURL("/mysql/data/export/downLoadAllFile.do?id=" + id + "&token=" + getUserToken())
};
function isNumberDataType(dataType) {
    return (dataType.indexOf("INT") >= 0 || dataType == "REAL" || dataType == "FLOAT" || dataType == "DOUBLE" || dataType == "NUMBER" || dataType == "NUMERIC")
}
function isBitDataType(dataType) {
    return (dataType == "BIT")
}
function isStringDataType(dataType) {
    return (dataType.indexOf("CHAR") >= 0 || dataType.indexOf("TEXT") >= 0)
}
function addRecord() {
    var fields = gridResult.store.model.getFields();
    var newRecord = gridResult.store.add({
        "__new": 1
    })[0];
    gridResult.getView().scrollBy(0, 10000000);
    gridResult.getView().refresh();
    gridResult.getSelectionModel().select(gridResult.store.getCount() - 1);
    for (var i = 0; i < fields.length; i++) {
        if (gridResult.editTableName == fields[i].tableName && gridResult.editDbName == fields[i].dbName) {
            var columnName = fields[i].realName;
            var defaultValue = getColumnDefaultValue(columnName);
            if (defaultValue != undefined) {
                newRecord.set(fields[i].name, defaultValue)
            }
        }
    }
}
function deleteRecord() {
    var selection = gridResult.getSelectionModel().getSelection();
    if (selection.length <= 0) {
        Ext.MessageBox.alert("提示", "请选择要删除的记录");
        return
    }
    for (var i = 0; i < selection.length; i++) {
        gridResult.store.remove(selection[i])
    }
}
function setColumnEditor() {
    var dbName = nowDBName;
    var tableName = objectName;
    var fields = gridResult.store.model.getFields();
    var columns = gridResult.columnManager.getColumns();
    for (var i = 0; i < columns.length; i++) {
        var column = columns[i];
        if (i > 0) {
            for (var j = 0; j < fields.length; j++) {
                var field = fields[j];
                if (field.name == column.dataIndex) {
                    if (!field.readOnly && field.tableName.replaceAll("`","") == tableName && field.dbName == dbName) {
                        if (isNumberDataType(field.dataType) && !Ext.isIE) {
                            column.setEditor({
                                xtype: "numberfield",
                                decimalPrecision: 20
                            })
                        } else {
                            if (field.dataType == "DATE") {
                                column.setEditor("idbdatefield")
                            } else {
                                if (field.dataType == "DATETIME" || field.dataType == "TIMESTAMP") {
                                    column.setEditor("datetimefield")
                                } else {
                                    if (field.dataType.indexOf("BLOB") >= 0) {
                                        column.setEditor(null)
                                    } else {
                                        if (field.size >= 10000) {
                                            column.setEditor("idbtextarea")
                                        } else {
                                            column.setEditor("mixEditor")
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
function getDeleteRecordSql(dbName,tableName) {
    var allSql = new Array();
    var records = gridResult.getStore().getRemovedRecords();
    for (var i = 0; i < records.length; i++) {
        var whereStr = "";
        var oldColumns = records[i].modified;
        var newColumns = records[i].data;
        for (var j = 0; j < records[i].fields.length; j++) {
            var field = records[i].fields.items[j];
            var columnName = field.name;
            var rowIndex = gridResult.getStore().indexOf(records[i]);
            var realName = field.realName;
            if (realName && v_primaryKeys.length > 0 && v_primaryKeys.indexOf(realName) != -1) {
                if (whereStr != "") {
                    whereStr = whereStr + " and "
                }
                var pkOldValue = oldColumns[columnName];
                if (pkOldValue == undefined) {
                    pkOldValue = records[i].data[columnName]
                }
                if (isNoZeroEmpty(pkOldValue)) {
                    Ext.Msg.alert("提示", "第" + (rowIndex + 1) + "行主键[" + field.realName + "]不能为空！");
                    return
                }
                whereStr = whereStr + "`" + field.realName + "`=" + getFieldValueStr(pkOldValue, field.dataType)
            }
        }
        var sql = "delete from " +dbName+"."+ tableName + " where " + whereStr + ";";
        allSql.push({
            rowIndex: rowIndex,
            type: "D",
            sql: sql
        })
    }
    return allSql
}
function getInsertRecordSql(dbName,tableName) {
    var allSql = new Array();
    var records = gridResult.getStore().getModifiedRecords();
    for (var i = 0; i < records.length; i++) {
        var sql = "";
        var oldColumns = records[i].modified;
        var newColumns = records[i].getChanges();
        if (records[i].raw.__new == 1) {
            var insertColumnNames = "";
            var insertColumnValues = "";
            var rowIndex = gridResult.getStore().indexOf(records[i]);
            for (var j = 0; j < records[i].fields.length; j++) {
                var field = records[i].fields.items[j];
                var columnName = field.name;
                if (oldColumns[columnName] !== undefined) {
                    if (insertColumnNames != "") {
                        insertColumnNames = insertColumnNames + ",";
                        insertColumnValues = insertColumnValues + ","
                    }
                    insertColumnNames = insertColumnNames + "`" + field.realName + "`";
                    insertColumnValues = insertColumnValues + getFieldValueStr(newColumns[columnName], field.dataType)
                }
            }
            if (insertColumnNames != "") {
                sql = "insert " + dbName+"."+tableName + "(" + insertColumnNames + ") values(" + insertColumnValues + ");";
                allSql.push({
                    rowIndex: rowIndex,
                    type: "I",
                    sql: sql
                })
            }
        }
    }
    return allSql
}
function getUpdateRecordSql(dbName,tableName) {
    var allSql = new Array();
    var records = gridResult.getStore().getModifiedRecords();
    for (var i = 0; i < records.length; i++) {
        var sql = "",
        field, columnName;
        var oldColumns = records[i].modified;
        var newColumns = records[i].getChanges();
        if (records[i].raw.__new != 1) {
            for (var j = 0; j < records[i].fields.length; j++) {
                field = records[i].fields.items[j];
                columnName = field.name;
                if (oldColumns[columnName] !== undefined) {
                    if (sql != "") {
                        sql = sql + ","
                    }
                    sql = sql + "`" + field.realName + "`=" + getFieldValueStr(newColumns[columnName], field.dataType)
                }
            }
            if (sql != "") {
                var whereStr = "";
                var rowIndex = gridResult.getStore().indexOf(records[i]);
                for (var k = 0; k < records[i].fields.length; k++) {
                    field = records[i].fields.items[k];
                    columnName = field.name;
                    var realName = field.realName;
                    if (realName && v_primaryKeys.length > 0 && v_primaryKeys.indexOf(realName) != -1) {
                        if (whereStr != "") {
                            whereStr = whereStr + " and "
                        }
                        var pkOldValue = oldColumns[columnName];
                        if (pkOldValue == undefined) {
                            pkOldValue = records[i].data[columnName]
                        }
                        if (isNoZeroEmpty(pkOldValue)) {
                            Ext.Msg.alert("提示", "第" + (rowIndex + 1) + "行主键[" + field.realName + "]不能为空！");
                            return
                        }
                        whereStr = whereStr + "`" + field.realName + "`=" + getFieldValueStr(pkOldValue, field.dataType)
                    }
                }
                sql = "update " + dbName+"."+tableName + " set " + sql + " where " + whereStr + ";";
                allSql.push({
                    rowIndex: rowIndex,
                    type: "U",
                    sql: sql
                })
            }
        }
    }
    return allSql
}
//展示编辑sql
function showEditDataSql() {
    var tableName = "`" + objectName.replaceAll("`", "") + "`";
    var dbName = "`" + nowDBName + "`";
    var allSql = new Array();
    var deleteSql = getDeleteRecordSql(dbName,tableName);
    var insertSql = getInsertRecordSql(dbName,tableName);
    var updateSql = getUpdateRecordSql(dbName,tableName);
    allSql = allSql.concat(deleteSql, insertSql, updateSql);
    if (allSql.length == 0) {
        return Ext.MessageBox.alert("提示", "没有数据被修改！")
    }
    var allSqlText = new Array();
    for (var i = 0; i < allSql.length; i++) {
        allSqlText.push(allSql[i].sql)
    }
    var title = "本次操作明细：";
    if (deleteSql.length > 0) {
        title = title + "delete:" + deleteSql.length + "条；"
    }
    if (insertSql.length > 0) {
        title = title + "insert:" + insertSql.length + "条；"
    }
    if (updateSql.length > 0) {
        title = title + "update:" + updateSql.length + "条；"
    }
    var win = Ext.create("Ext.window.Window", {
        title: title + "以下是提交执行的SQL，请确认",
        id: "win_change_sql",
        height: 300,
        width: 800,
        modal: true,
        maximizable: true,
        layout: "fit",
        items: {
            xtype: "codemirror",
            readOnly: true,
            value: allSqlText.join("\n")
        },
        buttonAlign: "center",
        buttons: [{
            text: "确定",
            handler: function() {
                postChange(allSql)
            }
        },
        {
            text: "取消",
            handler: function() {
                win.close()
            }
        }]
    }).show()
}
function postChange(allSql) {
    var loadMask = new Ext.LoadMask(Ext.getCmp("win_change_sql"), {
        msg: "正在提交，请稍候......",
        removeMask: true
    });
    loadMask.show();
    var jsonAllSql = Ext.encode(allSql);
     var paramsValue;
        if(isLocal=='isLocal'){
            paramsValue={dataId: currentDataBaseId,dbName: nowDBName,sql: jsonAllSql,baseType:currentBaseType,dataSourceId:currentDbSourceId,dataSource:cuurentdataSource,isLocal:isLocal}
        }else{
            paramsValue={dataId: currentDataBaseId,dbName: nowDBName,sql: jsonAllSql,baseType:currentBaseType,dataSourceId:currentDbSourceId,dataSource:cuurentdataSource}
        }
    Ext.Ajax.request({
        url: baseUrl + "/postChange",
        params: paramsValue,
        method: "POST",
        timeout: 600000,
        success: function(response, options) {
            loadMask.hide();
            var rs = Ext.decode(response.responseText);
            if (rs.failure) {
                Ext.MessageBox.alert("执行出错", rs.root)
            } else {
                var records = gridResult.getStore().getNewRecords();
                for (var i = 0; i < records.length; i++) {
                    if (records[i].raw.__new == 1) {
                        records[i].raw.__new = 0
                    }
                }
                for (var i = 0; i < rs.root.length; i++) {
                    var record = gridResult.getStore().getAt(rs.root[i].rowIndex);
                    var fields = gridResult.store.model.getFields();
                    for (var j = 0; j < fields.length; j++) {
                        if (fields[j].isAutoIncrement && !record.get(fields[j].name)) {
                            record.set(fields[j].name, rs.root[i].lastInsertId);
                        }
                        break
                    }
                }
                gridResult.store.commitChanges();
                Ext.getCmp("win_change_sql").close()
            }
        },
        failure: function(response, options) {
            loadMask.hide();
            Ext.MessageBox.alert("失败", "执行出错，通常是SQL响应时间太长！")
        }
    })
}
function getColumnDefaultValue(columnName) {
    if (gridResult && gridResult.editTableColumnsMeta) {
        for (var i = 0; i < gridResult.editTableColumnsMeta.length; i++) {
            var column = gridResult.editTableColumnsMeta[i];
            if (columnName.toLowerCase() == column.FIELD.toLowerCase()) {
                return column.DEFAULT
            }
        }
    }
}
var lTrimZero = function(value) {
    var index = 0;
    for (; index < value.length; index++) {
        if (value.charCodeAt(index) != 0) {
            break
        }
    }
    if (index != 0) {
        return value.substring(index)
    }
    return value
};
function getFieldValueStr(value, dataType) {
    if (value == null) {
        return null
    } else {
        if (value == "CURRENT_TIMESTAMP" && dataType == "TIMESTAMP") {
            return value
        } else {
            if (dataType.indexOf("BINARY") != -1 && value.substring(0, 2) == "0x") {
                if (value == "0x") {
                    return "''"
                } else {
                    return value
                }
            } else {
                if (dataType == "BINARY") {
                    return "'" + lTrimZero(value) + "'"
                } else {
                    if (value === "" && !isStringDataType(dataType)) {
                        return null
                    } else {
                        if (isNumberDataType(dataType)) {
                            return value
                        } else {
                            if (isBitDataType(dataType)) {
                                return "b'" + value + "'"
                            } else {
                                return "'" + value.replaceAll("\\\\", "\\\\").replaceAll("'", "\\'") + "'"
                            }
                        }
                    }
                }
            }
        }
    }
};